package com.neusoft.ruangong.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.ruangong.domain.Conference;
import com.neusoft.ruangong.domain.Reservation;
import com.neusoft.ruangong.domain.Reservations;
import com.neusoft.ruangong.mapper.ReservationMapper;
import com.neusoft.ruangong.mapper.RoomMapper;
import com.neusoft.ruangong.service.ReservationService;
import com.neusoft.ruangong.service.UserService;
import com.neusoft.ruangong.util.TimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements ReservationService {
    @Resource
    ReservationMapper reservationMapper;
    @Resource
    RoomMapper roomMapper;
    @Resource
    private Time begintime;
    @Resource
    private UserService userService;
    @Resource
    private Time endtime;
    @Override
    public Integer modifyReservation(Long id) {
        return reservationMapper.modifyReservation(id);
    }

    @Override
    public List<Conference> queryConference(Long id) {
        return reservationMapper.queryConference(id);
    }

    @Override
    public List<Reservations> UntreatedReservation(Long index,Long limit) {
        Long pageIndex = index*limit;
        return reservationMapper.UntreatedReservation(pageIndex,limit);
    }

    @Override
    public List<Reservations> queryLinkReservation(String str) {
        return reservationMapper.queryLinkReservation(str);
    }

    @Override
    public Integer addConference(BigInteger userId, Long reservationid) {
        return reservationMapper.addConference(userId,reservationid);
    }

    @Override
    public Reservation queryReservation(String user, Date date, Time begintime) {
        return reservationMapper.queryReservation(user,date,begintime);
    }

    @Override
    @Transactional
    public Boolean addConference(String roomName, Reservation reservation, List<String> userNames) {
        Long room_id = roomMapper.queryRoomId(roomName);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String formatData = simpleDateFormat.format(reservation.getDate());
        List<Reservation> reservations = reservationMapper.queryTimeReservation(room_id,formatData);

        Date date = TimeUtil.addDayOfDate(reservation.getDate(), 1);
        reservation.setDate(date);
        if(reservations.size()!=0){
            for (Reservation rs : reservations) {
                //时间校验
                if(reservation.getBegintime().getTime()>=rs.getBegintime().getTime()&&reservation.getBegintime().getTime()<rs.getEndtime().getTime()){
                    return false;
                }
                if(reservation.getEndtime().getTime()>rs.getBegintime().getTime()&&reservation.getEndtime().getTime()<=rs.getEndtime().getTime()){
                    return false;
                }
                if(reservation.getEndtime().getTime()<=rs.getBegintime().getTime()&&reservation.getEndtime().getTime()>=rs.getEndtime().getTime()){
                    return false;
                }
            }
        }

        reservation.setRoomId(room_id);
        Integer num = reservationMapper.addReservation(reservation);
        if(num!=null){
            try {
                long code = Long.parseLong(reservation.getCode());
                long reservationId = reservationMapper.queryReservationId(code);
                for (String userName : userNames) {
                    BigInteger userId = userService.queryByName(userName);
                    addConference(userId,reservationId);
                }
                return true;
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }
    @Override
    public Integer queryUntreatedSize() {
        return reservationMapper.queryUntreatedSize();
    }

    @Override
    public List<Reservations> queryReservationByName(String userName, Long index, Long limit) {
        Long pageIndex = index*limit;
        List<Reservations> reservations = reservationMapper.queryReservationByName(userName,pageIndex,limit);
        return reservations;
    }

    @Override
    public Integer queryReservationCount(String userName) {
        return reservationMapper.queryReservationCount(userName);
    }

    @Override
    public List<Reservations> searchUserReservation(String roomName, Long userId) {
        return reservationMapper.searchUserReservation(roomName,userId);
    }

    @Override
    public Map queryAllUserTime(List<String> users, String data, Time begintime, Time endtime) {
        Map<String,String> map=null;
        String userstr="";
        for (String user : users) {
            Long userId = userService.queryByNameLong(user);
            List<Conference> reservations = reservationMapper.queryTimeConference(userId,data);
            if(reservations.size()!=0){
                for (Conference rs : reservations) {
                    if(begintime.getTime()>=rs.getBegintime().getTime()&&begintime.getTime()<rs.getEndtime().getTime()){
                        userstr+=user+" ";
                        break;
                    }else if(endtime.getTime()>rs.getBegintime().getTime()&&endtime.getTime()<=rs.getEndtime().getTime()){
                        userstr+=user+" ";
                        break;
                    }else if(begintime.getTime()<=rs.getBegintime().getTime()&&endtime.getTime()>=rs.getEndtime().getTime()){
                        userstr+=user+" ";
                        break;
                    }else {
                        ;
                    }
                }
            }
        }
        if(!userstr.equals("")){
            map=new HashMap<>();
            map.put("users",userstr+"存在时间冲突");
        }
        return map;
    }

    @Override
    public Integer modifyReservation1(Long id) {
        return reservationMapper.modifyReservation1(id);
    }

    @Override
    public List<Reservations> pageReservation(Long index, Long limit) {
        Long pageIndex = index*limit;
        return reservationMapper.pageReservation(pageIndex,limit);
    }

    @Override
    public Integer queryPageSize() {
        return reservationMapper.queryPageSize();
    }


//    @Override
//    @Transactional
//    public Boolean addConference(String roomName, Reservation reservation, List<String> userNames) {
//        Long room_id = roomMapper.queryRoomId(roomName);
//        reservation.setRoomId(room_id);
//        Integer num = reservationMapper.addReservation(reservation);
//        if(num!=null){
//            try {
//                long code = Long.parseLong(reservation.getCode());
//                long reservationId = reservationMapper.queryReservationId(code);
//                for (String userName : userNames) {
//                    BigInteger userId = userService.queryByName(userName);
//                    addConference(userId,reservationId);
//                }
//                return true;
//            } catch (NumberFormatException e) {
//                e.printStackTrace();
//                return false;
//            }
//        }
//        return false;
//    }
}
